#include "../include/touch.h"

void get_click(int *x, int *y)
{
    int fd_ts = open("/dev/input/event0", O_RDWR);
    if(fd_ts < 0)
    {
        perror("ts open failed");
        exit(EXIT_FAILURE);
    }

    int x_val = 0;
    int y_val = 0;
    struct input_event ts;
    int ret = 0;

    while(1)
    {
        memset(&ts, 0, sizeof(struct input_event));
        ret = read(fd_ts, &ts, sizeof(struct input_event));
        if(ret < 0)
        {
            printf("error\n");
        }

        if(ts.type == EV_ABS && ts.code == ABS_X)
        {
            x_val = 1;
            *x = ts.value;
        }

        if(ts.type == EV_ABS && ts.code == ABS_Y)
        {
            y_val = 1;
            *y = ts.value;
        }

        if(ts.type == EV_KEY && ts.code == BTN_TOUCH && ts.value == 1)
        {   
            if(x_val && y_val)
            {
                printf("x = %d, y = %d\n", *x, *y);
                break;
            }
        }
    }
}